package cn.kgc.ubagnet.service.cart;

import cn.kgc.ubagnet.mapper.CartMapper;
import cn.kgc.ubagnet.mapper.CartProMapper;
import cn.kgc.ubagnet.pojo.Cart;
import cn.kgc.ubagnet.pojo.CartExample;
import cn.kgc.ubagnet.pojo.CartPro;
import cn.kgc.ubagnet.pojo.CartProExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Resource
    CartProMapper cartpromapper;
    @Resource
    CartMapper cartMapper;
    //查询用户的购物车
    @Override
    public List<CartPro> CarttProList(Integer userid, Integer checkid){
        CartProExample example=new CartProExample();
        CartProExample.Criteria criteria=example.createCriteria();
        criteria.andUseridEqualTo(userid);
        if(checkid!=0){
            criteria.andChecktEqualTo(checkid);
        }
        List<CartPro> cartProList=cartpromapper.selectByExample(example);
        return cartProList;
    }

    @Override
    public Cart CartByid(Integer id) {
        return cartMapper.selectByPrimaryKey(id);
    }

    //添加到购物车
    @Override
    public int AddCart(Cart cart) {
        return cartMapper.insertSelective(cart);
    }

    @Override
    public int SaveCart(Cart cart) {
        return cartMapper.updateByPrimaryKeySelective(cart);
    }

    @Override
    public int delCart(List<Integer> idlist) {
        CartExample cartExample=new CartExample();
        cartExample.createCriteria().andIdIn(idlist);
        if(cartMapper.deleteByExample(cartExample)>0){
            return 1;
        }
        return 0;
    }
}
